<template>
    <el-main>
      <!--搜索栏 -->
      <el-form :model="searchParm" :inline="true" size="default">
        <el-form-item>
          <el-input
            v-model="searchParm.title"
            placeholder="请输入反馈名称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="searchBtn" icon="Search">搜索</el-button>
          <el-button @click="resetBtn" type="danger" plain icon="Close"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <!-- 表格 -->
      <el-table :height="tableHeight" :data="tableList" border stripe>
        <el-table-column prop="title" label="标题"></el-table-column>
        <el-table-column prop="content" label="内容"></el-table-column>
        <el-table-column prop="createTime" label="创建时间"></el-table-column>
        <el-table-column label="操作" width="220" align="center">
          <template #default="scope">
            <el-button
              type="danger"
              icon="Delete"
              size="default"
              @click="deleteBtn(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="sizeChange"
        @current-change="currentChange"
        :current-page.sync="searchParm.currentPage"
        :page-sizes="[10, 20, 40, 80, 100]"
        :page-size="searchParm.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="searchParm.total"
        background
      >
      </el-pagination>
    </el-main>
  </template>
  
  <script setup lang="ts">
  import { nextTick, onMounted, reactive, ref } from "vue";
  import { getListApi ,Suggestion,deleteApi} from "@/api/suggestion/index";
  import { ElMessage } from "element-plus";
  import useInstance from "@/hooks/useInstance";
  const {global} = useInstance()

  //搜索绑定对象
  const searchParm = reactive({
    currentPage: 1,
    pageSize: 10,
    title: '',
    total:0
  });
 
  //删除
  const deleteBtn = async(row: Suggestion) => {
    console.log(row);
    const confirm = await global.$myconfirm('确定删除该数据吗?')
    if(confirm){
      let res = await deleteApi(row.id)
      if (res && res.code == 200) {
          ElMessage.success(res.msg);
          //刷新表格
          getList()
        }
    }
  };
  //页容量改变时触发
  const sizeChange = (size: number) => {
    searchParm.pageSize = size;
    getList();
  };
  //页数改变时触发
  const currentChange = (page: number) => {
    searchParm.currentPage = page;
    getList();
  };
  //表格数据
  const tableList = ref([]);
  //获取表格数据
  const getList = async () => {
    let res = await getListApi(searchParm);
    if (res && res.code == 200) {
      tableList.value = res.data.records;
      searchParm.total = res.data.total;
    }
  };
  //搜索
  const searchBtn = () => {
    getList();
  };
  //重置
  const resetBtn = () => {
    searchParm.currentPage = 1;
    searchParm.title = "";
    getList();
  };
  //表格高度
  const tableHeight = ref(0);
  onMounted(() => {
    getList();
    nextTick(() => {
      tableHeight.value = window.innerHeight - 230;
    });
  });
  </script>
  
  <style scoped></style>
  